package Matrices;

/**
  *  4g3nt
  *  Copyright (C) 2008  Carlos Molinero y César Andrés
  *
  *  This program is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  *  the Free Software Foundation, either version 3 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
  *
  *    You should have received a copy of the GNU General Public License
  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  */
public class Vector {
    private double[] _vector;
    public Vector(int dimension) 
     {
        _vector=new double[dimension];
        for(int i=0; i< dimension; i++)
                _vector[i]=0.0;
     
    }
    /**
     * Constructora por vector
     * @param vector a copiar
     */
    public Vector(double[] vector) 
    {
        setVector(vector);
    }
    /**
     * Constructora por vector
     * @param vector a copiar
     */
    public Vector(Vector vector) 
    {
        setVector(vector._vector);
        
    }
    private void setVector(double[] vector)
    {
        _vector = new double[vector.length];
        for (int i = 0 ; i < vector.length; i++)
        {
            _vector[i] = vector[i];
        }
    }
    public static double productoEscalar(Vector a, Vector b){
        double resultado=0.0;
        if (a._vector.length!=b._vector.length) return resultado;
        for(int i=0; i<a._vector.length; i++){
            resultado+=a._vector[i]*b._vector[i];
        }
        return resultado;
    }
    public double modulo()
    {
        return Math.sqrt(productoEscalar(this, this));
    }
    
    @Override
    public String toString(){
        String texto=" ";
        for(int i = 0; i < _vector.length; i ++)
            texto+="\t "+(double)Math.round(1000*_vector[i])/1000;
        return texto+"\n";
  }

    public int getDimension()
    {
        return _vector.length;
    }
    public double getPosicion(int i)
    {
        if (i < _vector.length && i >=0)
        {
            return _vector[i];
        }
        else return 0.0f;
    }
}
